package com.dcbeida.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.userinfo.UserInfoContract;
import com.dcbeida.activity.userinfo.UserInfoPersenter;
import com.dcbeida.bean.Area;
import com.dcbeida.bean.UserInfo;
import com.dcbeida.utils.GsonUtil;
import com.dcbeida.widget.AreaWheelDialog;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 我的资料
 * Created by hp on 2016/10/12.
 */
public class ProfileActivity extends BaseActivity implements View.OnClickListener, UserInfoContract.View {


    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.et_ydm)
    TextView etYdm;
    @Bind(R.id.et_dlm)
    TextView etDlm;
    @Bind(R.id.et_sjh)
    TextView etSjh;
    @Bind(R.id.tv_zzqy)
    TextView tvZzqy;
    @Bind(R.id.ll_zzqy)
    LinearLayout llZzqy;
    @Bind(R.id.tv_khzy)
    TextView tvKhzy;
    @Bind(R.id.tv_lxdh)
    TextView tvLxdh;
    @Bind(R.id.tv_wx)
    TextView tvWx;
    @Bind(R.id.tv_yx)
    TextView tvYx;
    @Bind(R.id.tv_qq)
    TextView tvQq;
    @Bind(R.id.et_yd_id)
    TextView etYdId;


    private AreaWheelDialog areaWheelDialog;
    private Area currentProvince;
    private Area currentCity;
    private Area currentArea;
    String provinceS = "";
    String cityS = "";
    String areaS = "";
    private UserInfoPersenter mPersenter;
    private UserInfo.UserBean userBean;
    private UserInfo.CustomerserviceBean customerserviceBean;
    private UserInfo userInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.wd_info2;
    }

    @Override
    public void setData() {
        super.setData();

        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.wdzl);
        tvRight.setVisibility(View.GONE);
        tvRight.setText(R.string.bc);
        //地址
        if (!TextUtils.isEmpty(MyApplication.user.getUser().getRegioncode())) {
            String[] ssq = MyApplication.user.getUser().getRegioncode().split(",");
            for (int i = 0; i < ssq.length; i++) {
                if (i == 0) {
                    provinceS = ssq[0];
                } else if (i == 1) {
                    cityS = ssq[1];
                } else if (i == 2) {
                    areaS = ssq[2];
                }
            }

        }


        mPersenter = new UserInfoPersenter(this, this);
        mPersenter.getUserInfo("", "");
    }

    private void setSSQ() {
        if (areaWheelDialog == null) {
            areaWheelDialog = new AreaWheelDialog(this);
            areaWheelDialog.setListener(new AreaWheelDialog.AreaSelectListener() {
                @Override
                public void area_selected(Area province, Area city, Area area) {
                    currentProvince = province;
                    currentCity = city;
                    currentArea = area;


                    String provinceStr = "";
                    String cityStr = "";
                    String areaStr = "";
                    if (!TextUtils.isEmpty(province.getName()) && !"市辖区".equalsIgnoreCase(province.getName()) && !"县".equalsIgnoreCase(province.getName()))
                        provinceStr = province.getName() + ",";
                    provinceS = province.getName();
                    if (city != null && !TextUtils.isEmpty(city.getName()) && !"市辖区".equalsIgnoreCase(city.getName()) && !"县".equalsIgnoreCase(city.getName()))
                        cityStr = city.getName() + ",";
                    cityS = city.getName();
                    if (area != null && !TextUtils.isEmpty(area.getName()) && !"市辖区".equalsIgnoreCase(area.getName()) && !"县".equalsIgnoreCase(area.getName()))
                        areaStr = area.getName();
                    areaS = area.getName();


                    tvZzqy.setText(provinceStr + cityStr + areaStr);
                }
            });
        }

        if (currentProvince != null && currentCity != null && currentArea != null) {
            areaWheelDialog.showDialog(provinceS, cityS, areaS);
        } else {
            areaWheelDialog.showDialog(provinceS, cityS, areaS);
        }
    }


    @OnClick({R.id.img_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:

                userBean = new UserInfo.UserBean();
                userBean.setId(userInfo.getUser().getId());
                userBean.setShopid(userInfo.getUser().getShopid());
                userBean.setUid(userInfo.getUser().getUid());
                userBean.setShopname(userInfo.getUser().getShopname());
                userBean.setTruename(etDlm.getText().toString().trim());
                userBean.setTel(etSjh.getText().toString().trim());
                userBean.setRegioncode(tvZzqy.getText().toString().trim());
                userBean.setCustomerid(userInfo.getUser().getCustomerid());
                userBean.setMobile(userInfo.getUser().getMobile());
                userBean.setRole(userInfo.getUser().getRole());
                userBean.setUsername(userInfo.getUser().getUsername());
                customerserviceBean = new UserInfo.CustomerserviceBean();
                customerserviceBean.setCustomerserviceid(userInfo.getCustomerservice().getCustomerserviceid());
                customerserviceBean.setCreatetime(userInfo.getCustomerservice().getCreatetime());

                String userStr = GsonUtil.GsonString(userBean);
                String customerserviceStr = GsonUtil.GsonString(customerserviceBean);

                mPersenter.setData(userStr, customerserviceStr);
                break;
//            case R.id.ll_zzqy:
//                setSSQ();
//                break;
        }
    }

    @Override
    public void setPresenter(UserInfoContract.Persent presenter) {

    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
        finish();
    }

    @Override
    public void showUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;

        etYdm.setText(userInfo.getUser().getShopname());
        etYdId.setText(userInfo.getUser().getShopid());
        etDlm.setText(userInfo.getUser().getTruename());
        etSjh.setText(userInfo.getUser().getTel());
        tvZzqy.setText(userInfo.getUser().getRegioncode() + userInfo.getUser().getAddress());


        tvKhzy.setText(userInfo.getCustomerservice().getName());
        tvLxdh.setText(userInfo.getCustomerservice().getPhone());
        tvWx.setText(userInfo.getCustomerservice().getWeixin());
        tvYx.setText(userInfo.getCustomerservice().getEmail());
        tvQq.setText(userInfo.getCustomerservice().getQq());

    }
}


